﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Live;

namespace SyncStories
{
    public partial class SkyDriveLogin : PhoneApplicationPage
    {
        public SkyDriveLogin()
        {
            InitializeComponent();
            Loaded += SkyDriveLogin_Loaded;
        }

        void SkyDriveLogin_Loaded(object sender, RoutedEventArgs e)
        {
            btnSignin.Click += btnSignin_Click;
        }

        private async void btnSignin_Click(object sender, RoutedEventArgs e)
        {
            bool connected = false;
            try
            {
                var authClient = new LiveAuthClient();
                LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" });

                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    connected = true;
                    var connectClient = new LiveConnectClient(result.Session);
                    var meResult = await connectClient.GetAsync("me");
                    dynamic meData = meResult.Result;
                    //updateUI(meData);
                }
            }
            catch (LiveAuthException ex)
            {
                // Display an error message.
            }
            catch (LiveConnectException ex)
            {
                // Display an error message.
            }

            // Turn off the display of the connection button in the UI.
            //btnSignin.Visibility = connected ? Visibility.Collapsed : Visibility.Visible;
            if (connected == true)
            {
                btnSignin.IsEnabled = false;
            }
        }
        

        //private async void btnSignIn_Click(object sender, RoutedEventArgs e)
        //{
        //    bool connected = false;
        //    try
        //    {
        //        var authClient = new LiveAuthClient();
        //        LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" });

        //        if (result.Status == LiveConnectSessionStatus.Connected)
        //        {
        //            connected = true;
        //            var connectClient = new LiveConnectClient(result.Session);
        //            var meResult = await connectClient.GetAsync("me");
        //            dynamic meData = meResult.Result;
        //            //updateUI(meData);
        //        }
        //    }
        //    catch (LiveAuthException ex)
        //    {
        //        // Display an error message.
        //    }
        //    catch (LiveConnectException ex)
        //    {
        //        // Display an error message.
        //    }

        //    // Turn off the display of the connection button in the UI.
        //    btnSignIn.Visibility = connected ? Visibility.Collapsed : Visibility.Visible; 
        //}

       //private async void connectButton_Click(object sender, RoutedEventArgs e)
       //{
       //    bool connected = false;
       //    try
       //    {
       //        var authClient = new LiveAuthClient();
       //        LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" });

       //        if (result.Status == LiveConnectSessionStatus.Connected)
       //        {
       //            connected = true;
       //            var connectClient = new LiveConnectClient(result.Session);
       //            var meResult = await connectClient.GetAsync("me");
       //            dynamic meData = meResult.Result;
       //            //updateUI(meData);
       //        }
       //    }
       //    catch (LiveAuthException ex)
       //    {
       //        // Display an error message.
       //    }
       //    catch (LiveConnectException ex)
       //    {
       //        // Display an error message.
       //    }

       //    // Turn off the display of the connection button in the UI.
       //    connectButton.Visibility = connected ? Visibility.Collapsed : Visibility.Visible; 
       //}
    }
}